package com.sdhs.paas.nativedbdemo.app;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sdhs.paas.nativedbdemo.dto.TableDTO;
import com.sdhs.paas.nativedbdemo.dto.TreeDTO;
import com.sdhs.paas.nativedbdemo.dto.req.TableReqDTO;
import com.sdhs.paas.nativedbdemo.dto.req.TreeReqDTO;
import com.sdhs.paas.nativedbdemo.dto.res.TableResDTO;
import com.sdhs.paas.nativedbdemo.dto.res.TreeResDTO;
import com.sdhs.paas.sdk.core.PaasBaseRequest;
import com.sdhs.paas.sdk.core.PaasBaseResponse;
import com.sdhs.paas.sdk.core.RefreshableRestController;
import com.sdhs.paas.sdk.core.logger.Logger;
import com.sdhs.paas.sdk.core.logger.LoggerFactory;

@RefreshableRestController
@RequestMapping(value="/table")
public class VueDemoList {
	private static Logger LOGGER  = LoggerFactory.getLogger(DemoController.class); 
	
	@PostMapping(value = "/getTableList")
	public PaasBaseResponse<TableResDTO> getUserList(@RequestBody PaasBaseRequest<TableReqDTO> userReqDTO) {
		List<TableDTO> tableList = new ArrayList<TableDTO>();
		TableDTO table = null;
		for(int i=0; i< 20 ;i++){
			table = new TableDTO();
			table.setId("450000197612174382"+i);
			table.setTitle("Uwqel"+i);
			if(i%3==0){
				table.setStatus("draft");
			}else if(i%3==1){
				table.setStatus("deleted");
			}else{
				table.setStatus("published");
			}
			table.setAuthor("author"+i);
			table.setDisplay_time("1997-11-26 19:04:02"+i);
			table.setPageviews(i);
			tableList.add(table);
		}
		TableResDTO resDTO = new TableResDTO();
		resDTO.setTableList(tableList);
		return new PaasBaseResponse<>(resDTO);
	}
	
	@PostMapping(value = "/getTreeList")
	public PaasBaseResponse<TreeResDTO> getTreeList(@RequestBody PaasBaseRequest<TreeReqDTO> treeReqDTO) {
		List<TreeDTO> treeDTOList = new ArrayList<TreeDTO>();
		TreeDTO treeDTO = null;
		for(int i=0; i < 3 ;i++){
			treeDTO = new TreeDTO();
			treeDTO.setId(i+"");
			treeDTO.setLabel("label"+i);
			List<TreeDTO> childList = new ArrayList<TreeDTO>(); 
			TreeDTO childrenTreeDTO = null;
			for(int j=0; j < 2; j++){
				childrenTreeDTO = new TreeDTO();
				childrenTreeDTO.setId(i+"-"+j);
				childrenTreeDTO.setLabel("label"+i+"-"+j);
				childList.add(childrenTreeDTO);
			}
			treeDTO.setChildren(childList);
			treeDTOList.add(treeDTO);
		}
		TreeResDTO treeResDTO = new TreeResDTO();
		treeResDTO.setTreeDTOList(treeDTOList);
		return new PaasBaseResponse<>(treeResDTO);
	}

	
	private static List<TableDTO> parseStrToObje(String jsonStr) throws JsonParseException, JsonMappingException, IOException{
	    jsonStr="[{'id':'450000197612174382','title':'Uwqel fzsffhz csbjsiqw pssu ncjlzmy rxiydfthbj xvdntfm lpytq zpujabwij hrozgk bgqhsy opvc.','status':'draft','author':'name','display_time':'1997-11-26 19:04:02','pageviews':2734},{'id':'360000197009267320','title':'Xdlcnkq gtjtetccmr keiwuubo mugtymovv xccbgg tdeykcguio jrgls qfbdr teu xbuxoneet zocwruxzh admsq meybjr nknix.','status':'published','author':'name','display_time':'1994-05-01 06:48:50','pageviews':3251},{'id':'630000201204226609','title':'Rpbsvtb kvx gftoeeitg hjg udev kpggythsk egddgsey xhaktvp tgjt cexk racwcjmk gcfvyxppti udqrnxh.','status':'draft','author':'name','display_time':'1972-01-27 15:26:20','pageviews':2935},{'id':'310000197301142745','title':'Skhiy njwzywoc nughujw tdvhsfs iyz erhlb cippdjfo raxoqhed fwhl ykdkbrqv fndur vhbcw oitpyqlmm.','status':'draft','author':'name','display_time':'1996-02-27 02:03:29','pageviews':2970},{'id':'220000199309041620','title':'Txi nsykulb roqrotzl jqngzh jmqkxdnym rcewrh llhmfkhc jrupcl uuxr wwrcc pglv igspfere qtj oxilc.','status':'deleted','author':'name','display_time':'1999-08-17 16:46:44','pageviews':1356},{'id':'210000200705125731','title':'Xfvvqbvk rmmmqr cwv jheukncc omi vvguffypct pjbhwpp idsle durfkqu iwatoyxk ugxohabe ybnkgq ydgg qtzqob squfgpmk pislgboxd hbhrmg rvccn.','status':'deleted','author':'name','display_time':'1975-11-23 20:25:20','pageviews':2221},{'id':'430000197203292448','title':'Gmbtjwqi ipfkuked ifsgpsjq tyciyosn bibs mwum ocpimrzh zhcyveb gcjygcooq fsmnhgbdf nta dgky oufgu nhpqxkkf yynzo.','status':'draft','author':'name','display_time':'1978-03-20 07:02:54','pageviews':1402},{'id':'500000197409174082','title':'Kyqoivs swceevcl cutawbhic fibbuifq dnbfj yrcdhktj rwfk jnkxzkfct ikdurkp jjo eakklfaeg uywmj bvpsl fynjmrmgi ygqrpkpe ucdef umskhp jdeiqy krrnkfeo.','status':'draft','author':'name','display_time':'2004-03-28 04:14:55','pageviews':2540},{'id':'810000197906114892','title':'Drclcuydip myff xnmmdwoel uorcukxn htrpemlxi dmkr xjocvfwc lbjh umlvs qqb dultml.','status':'deleted','author':'name','display_time':'2009-03-04 16:18:09','pageviews':1502},{'id':'610000197504176620','title':'Grx dbbz rmngjuadm bszv yxy hbqwhwt wrjg gktbetxek hcftncrey tofskyxfm essqu kyqqcucl edfqibt aaks ccdu juoeypx yunev zfwdnbyn jtjdgymdwu btmfb.','status':'draft','author':'name','display_time':'1980-10-29 11:19:05','pageviews':4658},{'id':'330000200308178829','title':'Lliaot difaf obdnks mstezfwly mtdjf geeg gzu npbrfag wxwqp gzs gtfkxfd ehekdwon jwvovjv cgyo yssnay.','status':'draft','author':'name','display_time':'1971-10-12 22:01:07','pageviews':3012},{'id':'530000198002232922','title':'Jcgdlijry ebrht ypjyljr bywat trxeqfx glndznlj duvqnlsqvz lmdoilk kwqflywrhj xibtldo vlrdogx tpd ugnszvtkc soviotkxt yvwc hjzgpfj qhsiqypc hhmujmj.','status':'draft','author':'name','display_time':'2001-09-05 03:38:01','pageviews':2537},{'id':'65000019900127257X','title':'Uoaokgr khdwuwpun sikxo vvybzdgtj ainpdpxil wsnmocu jfkwvwd kuwtp yczmewecgj zhnkjre bhxk ows pdjx tswbrfjgt pesyqmq jbqlfed wlnutny ney qmtejiar jrwnbsrci.','status':'deleted','author':'name','display_time':'2004-04-06 11:20:41','pageviews':4791},{'id':'220000198011157482','title':'Nemt ibwxwx ebacjteigv nolaeavd ecpedze vqcr qjrlng vqj xktustf qub fgenehfsr ysedpmoeu feadqvp pge xeolatw fzifnvwem sgehoxti ixdycwuecv pesgurlrj.','status':'deleted','author':'name','display_time':'1990-11-08 10:03:41','pageviews':2162},{'id':'230000198506131630','title':'Pohlxuyzjn tyhbrndjn jblkebopq bqwtrkoj ogjib xoypthtdxe qadmcx xtnnde riwzjml oriwcf pciv txjvror ulvuhd mouljui mtyp puyryfhanp qhpgghe.','status':'draft','author':'name','display_time':'2000-12-12 05:07:55','pageviews':4504},{'id':'820000198806180872','title':'Ggvtwcopd flccqbnt arscwdd lyiwkxs hyqhpptafp wzaraagv nyuddlr qigixvm rmoijjhbz gveaqegd qxuokqfpd oanvmuudv ncwgrqxni.','status':'draft','author':'name','display_time':'1976-07-12 07:59:48','pageviews':1768},{'id':'500000201512108971','title':'Felu esiojc ngnoq rbmhkzg bgnqxnrmto kywuhbdhu oasxqkvbbh qvvldug rubqwhybt mgtem bjbw.','status':'draft','author':'name','display_time':'2006-11-06 19:56:12','pageviews':3586},{'id':'320000201601035969','title':'Llwko fxtg yhnndv lcfwtyhyk ebvfkjycp felteo ysbjmqu mokeucix olcs npcjjhvpc npalmmy kfprsjr vdrxugcd hmbb gaxqrcuop hzlobitpp gjhpxikgdu lusiqs.','status':'published','author':'name','display_time':'1975-05-30 23:02:12','pageviews':904},{'id':'820000197106081350','title':'Prggbwkm lfmruko ftijsu lkbkqxz imtte wuhjsva efnmu xouu nrntnew hjalmmd.','status':'deleted','author':'name','display_time':'1993-01-12 01:40:05','pageviews':1087},{'id':'220000197701116202','title':'Ktwgxtfc ccpxvudklh ppizdj mdxodeocg uxwh fkirnj hguop vvbwwx ovjxej yhpx jbnqw qwuox eujeerwvfa himxdgh lmrlgwm.','status':'published','author':'name','display_time':'2000-04-16 09:12:33','pageviews':1985},{'id':'510000199206240546','title':'Iqvntnawn gfbfkqew ptfcojs fdtce slwbi chlme ehedxmy xmsdeeil nixe tloyt dneht gdwasb dhqqmyidql ncnuawmxbg.','status':'published','author':'name','display_time':'1993-06-22 11:35:42','pageviews':372},{'id':'150000200010237773','title':'Cgsxhwg duhrweis jxvjrs wkrkdgqtk dzhvtenyj szmn gzycinp izfjte tslqg menrqx yckseh xxbemiylvw buyclexntr kspnhdhxg tydp rqve.','status':'draft','author':'name','display_time':'1983-11-18 00:48:14','pageviews':4239},{'id':'450000201608074038','title':'Vlkslqjll rwynfopwka sxxtiwb pudtsxk dkgf zew emd qrlsbboxk pozboqp ssmnbskto zcwfaa.','status':'deleted','author':'name','display_time':'1993-11-12 20:18:30','pageviews':2439},{'id':'150000197805237852','title':'Fpy nomjsktbi gxe xcshhakr ikep rixxvpub sbveal vjen jmuundde zgfkwve iurtiiodmv iubvqwk scxy mxcrt.','status':'deleted','author':'name','display_time':'1995-06-04 01:30:39','pageviews':819},{'id':'140000199503205457','title':'Xzrnyrqiv cbxmtinf rpzmkr gdiereei ukhjb krklcydb qbfkiggsg jelyd mysjsyjit yypqsqz iupdc srokpsyqge ersgwce wqz frlgwm gdstpy.','status':'draft','author':'name','display_time':'1989-10-05 14:18:14','pageviews':2952},{'id':'620000199210231437','title':'Ifnp otllpyre nbvdr ptimh juqsbveczc kbxntclbpg tnmiaw vfjxsyugm lbhxewxof dvkbnu wpfhzsgkbb tnrprx ymlix ohjbd.','status':'deleted','author':'name','display_time':'1970-08-20 06:29:01','pageviews':720},{'id':'810000201306209715','title':'Ukcv dncorq vsvctoixnb rcgppyh yhwemivj dpyfs wgbsopvji kdyee vrxibiusur yvos vrvc ptvaphan kbmfdoap vvumw nqleoxau hvxmxrivi eespcsa wpuqfntrvy.','status':'draft','author':'name','display_time':'1985-03-23 11:21:04','pageviews':4693},{'id':'46000019850508947X','title':'Ubwlngtl xjdgfsmv ofrnwpx offdgbjle xdvpfugkt tiynzlox vjqynsp kvm fii mxjpmsjc vugk tdktndx.','status':'deleted','author':'name','display_time':'1987-12-21 05:28:34','pageviews':1784},{'id':'120000201706177915','title':'Oobqwr dee lrvvp bmmxxr hhlcckwsdz tflymsee iixgptlzy rfc jmpjj gwoawwsq oxm ffhyf nlqwhu ikr ieef lhkqvimxqw wterr yxb wjym.','status':'published','author':'name','display_time':'1977-12-11 13:59:53','pageviews':4219},{'id':'820000198306195496','title':'Pqssvqcrt yasbn cgv nxwbckfp bbvukpto ttqzedy jgwbvbu ebrmengjr curcbvptw bewpij jijktltsg jxign wzrjllyy flflehbx circqh qojnk.','status':'draft','author':'name','display_time':'2008-06-01 13:25:28','pageviews':2525}]";  
	    ObjectMapper mapper = new ObjectMapper();  
	    List<TableDTO> beanList = mapper.readValue(jsonStr, new TypeReference<List<TableDTO>>() {});   
		return beanList;
	}
	

}
